import router from './router'
import store from './store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from './utils/get-page-title'
import { asyncRoutes } from '@/router/index'
NProgress.configure({ showSpinner: false })
router.beforeEach((to, from, next) => {
  // console.log(to);
  document.title = getPageTitle(to.meta.title)
  next()
})
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  // console.log('路由守卫',to,from,next)
  // next()
  // 判断是否有token以及去的是不是登录页
  // 1.有token,去的是登录页  --->去主页
  // 2.没有token去的不是登录页  --->去主页

  // 白名单:不需要登录就可以直接访问的页面

  const whiteList = ['/login', '/404']
  const token = store.state.user.token
  if (token && to.path === '/login') {
    // console.log('存在token,不能去登录');
    next('/')
    NProgress.done()
  } else if (!token && whiteList.includes(to.path) === false) {
    // console.log('没有登陆&&访问的页面只能是/login,/404中的一个');
    next('/login')
    NProgress.done()
  } else {
    if (!store.state.user.userInfo.userId && token) {
      await store.dispatch('user/getUserInfo')
      const menus = store.state.user.userInfo.roles.menus
      const filterRoutes = menus.map(name => {
        return asyncRoutes.find(item => item.children[0].name === name)
      })
      // 添加动态路由
      router.addRoutes(filterRoutes)
      // 解决刷新时404bug
      filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
      // 将数据存储到vuex中
      store.commit('menu/setMenuList', filterRoutes)
      // 刷新时出现白屏问题
      next({
        ...to,
        replace: true// 重进一次,不保留重复历史
      })
    } else {
      next()
    }
  }
})
router.afterEach((to, from) => {
  NProgress.done()
})
// import store from './store'
// import { Message } from 'element-ui'
// import NProgress from 'nprogress' // progress bar
// import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
